﻿using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Default2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        UnobtrusiveValidationMode = UnobtrusiveValidationMode.None;

            if (!IsPostBack && Session["AdminId"] != null)
            {
                MyDataDataContext db = new MyDataDataContext();
            TimeNow.Text = DateTime.Now.ToString();
            //获取用户信息
            var results = from r in db.User
                              where r.UserID != null
                              select new
                              {
                                  r.UserID,
                                  r.UserNickname,
                                  r.UserPwd,
                                  r.StudentNumber,
                                  r.UserAddress,
                                  r.UserSex,
                                  r.UserPhone,
                                  r.QQ,
                                  r.StartNumber,
                                  r.Start,
                                  r.blockflag
                              };//映射需要显示的字段
            
            usergv.DataSource = results;
                //调用DataBind()方法显示信息
                usergv.DataBind();
        }
        else
        {
            Response.Redirect("../Login.aspx");
        }

    }

    protected void usergv_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        GridViewRow row = usergv.Rows[e.RowIndex];
        MyDataDataContext db = new MyDataDataContext();
        User DeleteObj = (from r in db.User
                          where r.UserID == row.Cells[0].Text
                          select r).FirstOrDefault();
        //删除
        db.User.DeleteOnSubmit(DeleteObj);
        db.SubmitChanges();
        string message = "删除成功！";
        string js = @"<Script language='JavaScript'>alert('" + message + "');window.close();</Script>";
        //获取用户信息
        var results = from r in db.User
                      where r.UserID != null
                      select new
                      {
                          r.UserID,
                          r.UserNickname,
                          r.UserPwd,
                          r.StudentNumber,
                          r.UserAddress,
                          r.UserSex,
                          r.UserPhone,
                          r.QQ,
                          r.StartNumber,
                          r.Start,
                          r.blockflag
                      };//映射需要显示的字段

        //设置GridView控件的数据源
        usergv.DataSource = results;
        //调用DataBind()方法显示信息
        usergv.DataBind();
        Response.Write(js);
        Page_Load(sender, e);
    }

    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
            GridViewRow row = (GridViewRow)((DropDownList)sender).NamingContainer;//获取按钮所在行，即获取要封禁/解封的用户行
            DropDownList ddl = new DropDownList();
            ddl = (DropDownList)row.FindControl("ddl");
        
            MyDataDataContext db = new MyDataDataContext();
            User user = (from r in db.User
                         where r.UserID == row.Cells[0].Text
                         select r).FirstOrDefault();

        user.blockflag = ddl.SelectedValue;//将对用户的操作写入用户标识
            db.SubmitChanges();//提交
    }
}